package com.tanhua.server.controller;

import com.tanhua.common.utils.PostNotAllowed;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import com.tanhua.server.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("movements")
public class QuanZiController {

    @Autowired
    private QuanZiService quanZiService;

    //好友动态
    @GetMapping
    public ResponseEntity movements(Integer page,Integer pagesize) {
        //调用service查询好友动态的方法
        PageResult result = quanZiService.queryFriendList(page, pagesize);

        return ResponseEntity.ok(result);

    }

    //推荐动态
    @GetMapping("recommend")
    public ResponseEntity recommend(Integer page,Integer pagesize){

        PageResult result=quanZiService.queryRecommendList(page,pagesize);

        return ResponseEntity.ok(result);
    }

    //发布动态
    @PostNotAllowed
    @PostMapping
    public ResponseEntity savePublish(@RequestParam("textContent") String textContent,
                                      @RequestParam(value = "location", required = false) String location,
                                      @RequestParam(value = "latitude", required = false) String latitude,
                                      @RequestParam(value = "longitude", required = false) String longitude,
                                      @RequestParam(value = "imageContent", required = false) MultipartFile[] multipartFile){

        quanZiService.savePublish(textContent, location, latitude, longitude, multipartFile);

        return ResponseEntity.ok(null);
    }


    //动态点赞
    @GetMapping("{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String publishId){
        Long likeCount = this.quanZiService.likeComment(publishId);
        return ResponseEntity.ok(likeCount);
    }

    //取消动态点赞
    @GetMapping("{id}/dislike")
    public ResponseEntity dislikeComment(@PathVariable("id") String publishId){
        Long likeCount = this.quanZiService.dislikeComment(publishId);
        return ResponseEntity.ok(likeCount);
    }

    //动态喜欢
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId) {
        try {
            Long loveCount = this.quanZiService.loveComment(publishId);
            if (null != loveCount) {
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //取消动态喜欢
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> disLoveComment(@PathVariable("id") String publishId) {
        try {
            Long loveCount = this.quanZiService.disLoveComment(publishId);
            if (null != loveCount) {
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //查询单条动态
    @GetMapping("/{id}")
    public ResponseEntity<QuanZiVo> queryById(@PathVariable("id") String publishId) {
        try {
            QuanZiVo movements = this.quanZiService.queryById(publishId);
            if(null != movements){
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //自己的所有动态
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        try {
            PageResult pageResult = this.quanZiService.queryAlbumList(userId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList(){
        try {
            List<VisitorsVo> list = this.quanZiService.queryVisitorsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
